package com.dankegongyu.component.jsbridge.config;


import androidx.fragment.app.FragmentActivity;

import com.dankegongyu.component.jsbridge.DKWebView;
import com.dankegongyu.component.jsbridge.jsbridge.CallBackFunction;
import com.dankegongyu.component.jsbridge.listener.DKWebCallback;

import java.util.Map;

/**
 * Created by Admin on 2018/4/2.
 */
public class DKWebService {

    private FragmentActivity mActivity;
    private DKWebView mDKWebView;
    private DKWebConfig mConfig;

    private DKWebService() {
    }

    static DKWebService create() {
        return new DKWebService();
    }

    /**
     * 配置上下文
     */
    public void setContext(FragmentActivity activity) {
        mActivity = activity;
    }


    /**
     * 获取上下文
     */
    public FragmentActivity getContext() {
        return mActivity;
    }


    /**
     * 配置webview
     */
    public void setWebview(DKWebView webView) {
        mDKWebView = webView;
    }


    /**
     * 获取webview
     */
    public DKWebView getWebview() {
        return mDKWebView;
    }


    /**
     * 配置文件
     */
    public void register(DKWebConfig config) {
        mConfig = config;
    }

    public void unregister() {

        if (mDKWebView != null) {
            try {
                mDKWebView.stopLoading();
                mDKWebView.removeAllViewsInLayout();
                mDKWebView.removeAllViews();
                mDKWebView.setWebViewClient(null);
                mDKWebView.setWebChromeClient(null);
                mDKWebView.destroy();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }

        mDKWebView = null;
        mConfig = null;
        mCallback = null;
        mJsFunction = null;
        mActivity = null;
    }

    public DKWebConfig getConfig() {
        return mConfig;
    }

    /**
     * 逻辑层通知到view层
     */
    private DKWebCallback mCallback;

    public void setCallback(DKWebCallback callback) {
        mCallback = callback;
    }

    public DKWebCallback getCallback() {
        return mCallback;
    }

    /**
     * js 回调接口
     */
    private CallBackFunction mJsFunction;

    public void setJsCallback(CallBackFunction callback) {
        mJsFunction = callback;
    }

    public CallBackFunction getJsCallback() {
        return mJsFunction;
    }

    /**
     * 额外数据
     */
    private double mQuality;

    public void setQuality(double quality) {
        mQuality = quality;
    }

    public double getQuality() {
        return mQuality;
    }


    /**
     * 业务码
     */
    private String mServiceLine;

    public void setServiceLine(String serviceLine) {
        mServiceLine = serviceLine;
    }

    public String getServiceLine() {
        return mServiceLine;
    }

    /**
     * 额外数据
     */
    private Map<String, String> extraMap;

    public void setExtraMap(Map<String, String> extraMap) {
        this.extraMap = extraMap;
    }

    public Map<String, String> getExtraMap() {
        return extraMap;
    }


}
